/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.scene.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.istep.common.config.Global;
import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.web.BaseController;
import com.lczyfz.istep.common.utils.StringUtils;
import com.lczyfz.istep.modules.scene.entity.SceneSubnet;
import com.lczyfz.istep.modules.scene.service.SceneSubnetService;

/**
 * 子网管理Controller
 * @author dh
 * @version 2017-03-25
 */
@Controller
@RequestMapping(value = "${adminPath}/scene/sceneSubnet")
public class SceneSubnetController extends BaseController {

	@Autowired
	private SceneSubnetService sceneSubnetService;
	
	@ModelAttribute
	public SceneSubnet get(@RequestParam(required=false) String id) {
		SceneSubnet entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sceneSubnetService.get(id);
		}
		if (entity == null){
			entity = new SceneSubnet();
		}
		return entity;
	}
	
	@RequiresPermissions("scene:sceneSubnet:view")
	@RequestMapping(value = {"list", ""})
	public String list(SceneSubnet sceneSubnet, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SceneSubnet> page = sceneSubnetService.findPage(new Page<SceneSubnet>(request, response), sceneSubnet); 
		model.addAttribute("page", page);
		return "modules/scene/sceneSubnetList";
	}

	@RequiresPermissions("scene:sceneSubnet:view")
	@RequestMapping(value = "form")
	public String form(SceneSubnet sceneSubnet, Model model) {
		model.addAttribute("sceneSubnet", sceneSubnet);
		return "modules/scene/sceneSubnetForm";
	}

	@RequiresPermissions("scene:sceneSubnet:edit")
	@RequestMapping(value = "save")
	public String save(SceneSubnet sceneSubnet, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, sceneSubnet)){
			return form(sceneSubnet, model);
		}
		sceneSubnetService.save(sceneSubnet);
		addMessage(redirectAttributes, "保存子网成功");
		return "redirect:"+Global.getAdminPath()+"/scene/sceneSubnet/?repage";
	}
	
	@RequiresPermissions("scene:sceneSubnet:edit")
	@RequestMapping(value = "delete")
	public String delete(SceneSubnet sceneSubnet, RedirectAttributes redirectAttributes) {
		sceneSubnetService.delete(sceneSubnet);
		addMessage(redirectAttributes, "删除子网成功");
		return "redirect:"+Global.getAdminPath()+"/scene/sceneSubnet/?repage";
	}

}